<?php ob_start(); ?> 
<?php header ('Content-type: text/xml; charset=utf-8'); ?>     

<?php include "constants.inc" ?>

<?php include "classes.inc" ?>

<?php session_start(); ?>

<?php

function storeVisitTeam() {

	if (isset($_GET['visitTeam'])) {

		$visitTeam = $_GET['visitTeam'];

	  	mysql_connect(DB_SERVER, DB_USER, DB_PWD);

	    mysql_select_db(DB_CATALOG);

	  	$query = sprintf("select team_id from zt_team te

	  		join zt_tournament t on t.tournament_id = te.tournament_id

	  		where t.iscurrent = 1 and team_name = '%s'", $visitTeam);

	  	$result = mysql_query($query);

	  	$record = mysql_fetch_assoc($result);



	  	$dep = $_SESSION['dep'];

		$round = $_SESSION['round'];

	  	$homeTeamId = $_SESSION['homeTeam'];

	  	$visitTeamId = $record['team_id'];



	  	if ( !empty($dep) && !empty($round) && !empty($homeTeamId) && !empty($visitTeamId)) {

	    	$query = sprintf("select group_id from zt_group g

	    		join zt_tournament t on t.tournament_id = g.tournament_id

	    		where t.iscurrent = 1 and g.group_name = '%s'", $dep);

    		$result = mysql_query($query);

      		$row = mysql_fetch_assoc($result);

      		$groupId = $row['group_id'];



      		$query = sprintf("select 1 from zt_game g join zt_calendar c on c.calendar_id =

        		g.calendar_id where g.group_id = %u and g.home_team = %u

        		and g.visit_team = %u and c.round_nr = %u",

        		$groupId, $homeTeamId, $visitTeamId, $round);

      		

        	$gameResult = mysql_query($query);

      		if (mysql_num_rows($gameResult) > 0) {

        		return '2';

      		}

	  	}



    	$_SESSION['visitTeam'] = $visitTeamId;



    	return '1';

  	}

  	return '0';

}

?>

<response>

  <method>storeVisitTeam</method>

  <result><?php

    echo storeVisitTeam()?></result>

</response>